using System;
using UnityEngine;

public class TopAdsAnchor : MonoBehaviour
{
	protected UIWidget mWidget;

	protected Transform mTransform;

	public bool onlyIncludeTopAnchor;

	protected UIRoot root;

	protected bool started;

	public float adHeight = 60f;

	public float screenDimensionFor2x = 800f;

	protected float usedAdHeight;

	public bool checkInGameAds;

	public float noAdHeight = 10f;

	private int maxCount;

	private void ScreenSizeChanged()
	{
		maxCount = 0;
		if (started)
		{
			Update();
		}
	}

	private void Awake()
	{
		root = NGUITools.FindInParents<UIRoot>(base.transform);
		mTransform = base.transform;
		UICamera.onScreenResize = (UICamera.OnScreenResize)Delegate.Combine(UICamera.onScreenResize, new UICamera.OnScreenResize(ScreenSizeChanged));
		NavigationManager.onLayerChanged += ScreenSizeChanged;
	}

	private void OnDestroy()
	{
		UICamera.onScreenResize = (UICamera.OnScreenResize)Delegate.Remove(UICamera.onScreenResize, new UICamera.OnScreenResize(ScreenSizeChanged));
		NavigationManager.onLayerChanged -= ScreenSizeChanged;
	}

	private void Start()
	{
		maxCount = 0;
		mWidget = GetComponent<UIWidget>();
		started = true;
		usedAdHeight = adHeight;
		if (checkInGameAds && !Ads.instance.shouldShowAds)
		{
			usedAdHeight = noAdHeight;
			Ads.instance.hideBanner(true);
		}
		if ((float)Mathf.Max(Screen.width, Screen.height) >= screenDimensionFor2x)
		{
			usedAdHeight *= 2f;
		}
		Update();
	}

	private void Update()
	{
		if (maxCount >= 1)
		{
			base.enabled = false;
			return;
		}
		maxCount++;
		int num = -(int)(usedAdHeight * (float)root.manualHeight / (float)Screen.height);
		if (num != 0)
		{
			int num2 = mWidget.topAnchor.absolute - mWidget.bottomAnchor.absolute;
			mWidget.topAnchor.absolute = num;
			if (!onlyIncludeTopAnchor)
			{
				mWidget.bottomAnchor.absolute = num - num2;
			}
		}
		mWidget.UpdateAnchors();
	}
}
